﻿


namespace WPF.TestEditor.Controls
{
    using System;

    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using ICSharpCode.AvalonEdit.Highlighting;
    using ICSharpCode.AvalonEdit;
    using ICSharpCode.AvalonEdit.Rendering;
    using ICSharpCode.AvalonEdit.Document;
    using System.Windows.Media;

    public class TextEditorEx : TextEditor
    {
        public event EventHandler<ColorizingLineEventArgs> HighlightLine;
        public void OnHighlightLine(ColorizingLineEventArgs data)
        {
            if (HighlightLine != null)
                HighlightLine(this, data);
        }
        protected override IVisualLineTransformer CreateColorizer(IHighlightingDefinition highlightingDefinition)
        {
            if (highlightingDefinition == null)
            {
                throw new ArgumentNullException("highlightingDefinition");
            }
            return new HighlightingValidatorColorizer(this, highlightingDefinition.MainRuleSet);
        }
    }

    public class HighlightingValidatorColorizer : HighlightingColorizer
    {
        private TextEditorEx curEditor;
        public HighlightingValidatorColorizer(TextEditorEx curEditor, HighlightingRuleSet ruleSet)
            : base(ruleSet)
        {
            this.curEditor = curEditor;
        }
        protected override void ColorizeLine(DocumentLine lineToFormat)
        {
            //foreach (var segment in curEditor.TextArea.Selection.Segments)
            //{
            //    int segmentStart = segment.StartOffset;
            //    int segmentEnd = segment.EndOffset;
            //    if (segmentStart == segmentEnd) continue;
            //    if (segmentEnd <= lineToFormat.Offset) continue;
            //    if (segmentStart >= lineToFormat.Offset + lineToFormat.Length) continue;
            //    return;
            //}
            base.ColorizeLine(lineToFormat);
            this.curEditor.OnHighlightLine(new ColorizingLineEventArgs()
            {
                line=lineToFormat,
                lineStartOffset = lineToFormat.Offset,
                LineText = CurrentContext.Document.GetText(lineToFormat),
                OnHighlightLineSegment = HighlightLineSegment
            });
        }
        public void HighlightLineSegment(int start, int end, HighlightingColor colorinfo)
        {
            base.ChangeLinePart(start, end, (VisualLineElement element) =>
            {
                Typeface tf = element.TextRunProperties.Typeface;
                element.TextRunProperties.SetTypeface(new Typeface(
                    tf.FontFamily,
                    colorinfo.FontStyle.HasValue ? colorinfo.FontStyle.Value : tf.Style,
                    colorinfo.FontWeight.HasValue ? colorinfo.FontWeight.Value : tf.Weight,
                    tf.Stretch
                ));
                if (colorinfo.Foreground != null) element.TextRunProperties.SetForegroundBrush(colorinfo.Foreground.GetBrush(null));
                if (colorinfo.Background != null) element.TextRunProperties.SetBackgroundBrush(colorinfo.Background.GetBrush(null));
            });
        }
    }

    public class ColorizingLineEventArgs : EventArgs
    {
        public int lineStartOffset;
        public string LineText;
        public Action<int, int, HighlightingColor> OnHighlightLineSegment;
        public DocumentLine line;
    }

}
